package com.ruoyi.system.service.impl;

import java.sql.Wrapper;
import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.bean.merchant.dto.AddMerchant;
import com.ruoyi.system.bean.merchant.query.MerchantQuery;
import com.ruoyi.system.domain.dto.MerchantDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MerchantMapper;
import com.ruoyi.system.domain.Merchant;
import com.ruoyi.system.service.IMerchantService;

/**
 * 商家Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-04
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements IMerchantService
{
    @Autowired
    MerchantMapper merchantMapper;

    /**
     * 查询商家
     * 
     * @param id 商家主键
     * @return 商家
     */
    @Override
    public Merchant selectMerchantById(Long id)
    {
        return merchantMapper.selectMerchantById(id);
    }

    /**
     * 查询商家列表
     * 
     * @param merchant 商家
     * @return 商家
     */
    @Override
    public List<Merchant> selectMerchantList(Merchant merchant)
    {
        return merchantMapper.selectMerchantList(merchant);
    }

    /**
     * 新增商家
     * 
     * @param addMerchant 商家
     * @return 结果
     */
    @Override
    public int insertMerchant(AddMerchant addMerchant)
    {
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(addMerchant,merchant);
        merchant.setCreateTime(DateUtils.getNowDate());
        return merchantMapper.insertMerchant(merchant);
    }

    /**
     * 修改商家
     * 
     * @param merchant 商家
     * @return 结果
     */
    @Override
    public int updateMerchant(Merchant merchant)
    {
        merchant.setUpdateTime(DateUtils.getNowDate());
        return merchantMapper.updateMerchant(merchant);
    }

    /**
     * 批量删除商家
     * 
     * @param ids 需要删除的商家主键
     * @return 结果
     */
    @Override
    public int deleteMerchantByIds(Long[] ids)
    {
        return merchantMapper.deleteMerchantByIds(ids);
    }

    /**
     * 删除商家信息
     * 
     * @param id 商家主键
     * @return 结果
     */
    @Override
    public int deleteMerchantById(Long id)
    {
        return merchantMapper.deleteMerchantById(id);
    }


    /**
    * 查询商家列表   (查询接口使用)
    *
    * @description:
      * @author: tanjialong
      * @date: 2024/3/22 10:21
      * @param merchantQuery
      * @return: List<Merchant>
    **/
    @Override
    public List<Merchant> selectMerList(MerchantQuery merchantQuery) {
        return merchantMapper.selectMerchantQueryList(merchantQuery);
    }

//    public Merchant getOne(MerchantDto merchant) {
//        LambdaQueryWrapper<Merchant> qw = Wrappers.lambdaQuery();
//        Optional.ofNullable(merchant.getId()).ifPresent(obj -> qw.eq(Merchant::getId, obj));
//        Optional.ofNullable(merchant.getUserId()).ifPresent(obj -> qw.eq(Merchant::getUserId, obj));
//        return merchantMapper.selectOne(qw);
//    }
}
